package com.lun.blog.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

public class MyBeanUtils {

    /**
     * 获取对象中的空字段数组
     * @param source
     * @return
     */
    public static String[] getNotNullPropertyName(Object source){
        BeanWrapper beanWrapper=new BeanWrapperImpl(source);
        PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
        List<String> list=new ArrayList<>();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            String name = propertyDescriptor.getName();
            if(beanWrapper.getPropertyValue(name)==null){
                list.add(name);
            }
        }
        return list.toArray(new String[list.size()]);
    }

}
